public without sharing class MeasureCalcController {


    public Measure_Calculate__c mc { get; private set; }
    public String mcId { get; private set; }


    public User u { get; private set ; }

    public Integer mcRight {get ;private set;} 

    public String paramJson {get;private set;} 

    public MeasureCalcController() {
        mcId = System.currentPageReference().getParameters().get('id');
        
    }
  

    public void init() {
        // 获取测算
        Schema.DescribeSobjectResult m = Measure_Calculate__c.sObjectType.getDescribe();
        Map<String, Schema.SObjectField> fieldMap = m.fields.getMap();
        
        String sql = 'select ';
        String fields = '';
        for (String field : fieldMap.keySet()) {
            if (fields.length() > 0) {
                fields += ', '; 
            }
            fields += field;
        }
        sql += fields;
      
///还有3个成本价格字段;
        sql +=', Opportunity__r.Account.Name , Opportunity__r.Account.Level__c,Opportunity__r.Account.LISRevenue__c,Opportunity__r.CoopDealer__r.Name,CoopDealer__r.Name,Opportunity__r.OwnerId'; 
         
        sql += ' from Measure_Calculate__c where Id = \'' + mcId + '\'';

        mc = Database.query(sql);


        
        u = [select Id,Name,SubCompany__c, Profile.Name from User where Id = :UserInfo.getUserId()];
        
        
      
        mcRight =  MeasureRightUtil.getRight(mc.Company__c);



       
    }
    // public void genExcel(){

    //    exceId = ImportObject.saveExelBlob(EncodingUtil.urlDecode(excelJson, 'UTF-8'));
    // }
    public void setNil(){
        paramJson = '{regent:[],equip:[],additional:[]}';
    }
    public void getDetails(){
        
        paramJson = getDetails(mcId);

    }

    public String getDetails(String mc_id){
        Map<String ,Object> tmp = new Map<String,Object>();
        ///测算试剂
        String sql = ' select TestNum__c,SalesPriceWT__c,UnitPriceWT__c ,SampleNum1__c ,TaxRate__c,SubInstCost__c ,InstCost__c , Product__r.Category2__c ,';
        sql += ' Product__r.Name, Product__r.SerialNo__c, Product__r.Brand__c,  Product__r.NCCode__c, Product__r.ProductName__c,Product__r.Category1__c ';
        sql += ' from Measure_Calculate_Product__c where Measure_Calculate__c = \'' + mc_id + '\' ';
        sql += ' and IsEquipRelated__c = false  order by  Product__r.SerialNo__c ';
       
        // 已经测算过，测算产品有数据
        List<MCProductBean> mcptmp = new List<MCProductBean>();
        for (Measure_Calculate_Product__c mcp : Database.query(sql)) {
            mcptmp.add(new MCProductBean(mcp));
        }
        tmp.put('regent', mcptmp);

        //测算仪器

        List<MCEquipmentBean> metmp = new List<MCEquipmentBean>();
        for(Measure_Calculate_Product__c mcp : [select Product__r.Name ,Product__r.ProductName__c,
        Product__r.SerialNo__c, Product__r.Brand__c,Product__r.Model__c ,SubInstCost__c,RepairCost__c, Product__r.Spec__c,
        InstCost__c,Quantity__c from Measure_Calculate_Product__c where Measure_Calculate__c =:mc_id 
        and  IsEquipRelated__c = true   ]){
            metmp.add(new MCEquipmentBean(mcp) );
        }
         tmp.put('equip', metmp);
        //测算增值服务
        List<AdditionalBean> adtmp = new List<AdditionalBean>();
        for(AdditionalService__c adds  : [select Id, Name, Cost__c from AdditionalService__c where MeasureCalc__c = :mc_id]){
            adtmp.add(new AdditionalBean(adds) );
        }
        tmp.put('additional', adtmp);

        return EncodingUtil.urlEncode(JSON.serialize(tmp), 'UTF-8');

    }
   
   

    


    public class AdditionalBean{
        public String serviceName;
        public Decimal cost;
        public AdditionalBean(AdditionalService__c record ){
            serviceName = record.Name;
            cost = record.Cost__c != null ? record.Cost__c.setScale(2, RoundingMode.HALF_UP) : null ; 
        }
    }

    public class MCEquipmentBean{

        public String productName;
        public String serialNo;
        public String model ; 
        public String spec ; 
        public String brand ; 
        public Decimal num ; 
        public Decimal subcost ; 
        public Decimal groupcost ; 
        public Decimal repaircost ; 
        public MCEquipmentBean(Measure_Calculate_Product__c  record ){
            productName = record.Product__r.ProductName__c == null ? record.Product__r.Name : record.Product__r.ProductName__c  ;

            serialNo = record.Product__r.SerialNo__c;
            brand = record.Product__r.Brand__c;
            model = record.Product__r.Model__c; 
            subcost =  record.SubInstCost__c == null ? null :record.SubInstCost__c.setScale(2, RoundingMode.HALF_UP);
            groupcost =  record.InstCost__c == null ? null :record.InstCost__c.setScale(2, RoundingMode.HALF_UP); 
            repaircost = record.RepairCost__c == null ? null : record.RepairCost__c.setScale(2, RoundingMode.HALF_UP); 
            num = record.Quantity__c;
            spec = record.Product__r.Spec__c;
        }

    }

    public class MCProductBean{
        public String productName;
        public String serialNo;
        public String nccode ; 
        public String brand ; 
        public Decimal testNum ; 
        public Decimal unitPrice; 
        public Decimal salesPrice;
        public Decimal tax;
        public Decimal sample0;
        public Decimal subcost ; 
        public Decimal groupcost  ; 
        public String cate1 ; 
        public String cate2 ; 


        public MCProductBean(Measure_Calculate_Product__c record){

            productName = record.Product__r.ProductName__c == null ? record.Product__r.Name : record.Product__r.ProductName__c  ;

            nccode = record.Product__r.NCCode__c;
            cate1 = record.Product__r.Category1__c;
            cate2 = record.Product__r.Category2__c;
            serialNo = record.Product__r.SerialNo__c;
            brand = record.Product__r.Brand__c;
            testNum = record.TestNum__c;
            unitPrice = record.UnitPriceWT__c;
            salesPrice = record.SalesPriceWT__c == null ? null :record.SalesPriceWT__c.setScale(2, RoundingMode.HALF_UP);
            tax = record.TaxRate__c;
            sample0 = Integer.valueOf(record.SampleNum1__c);
            subcost =  record.SubInstCost__c == null ? null :record.SubInstCost__c.setScale(2, RoundingMode.HALF_UP);
            groupcost =  record.InstCost__c == null ? null :record.InstCost__c.setScale(2, RoundingMode.HALF_UP); 

        }
    }


   

    
}